package com.zhai.boot.service;

import com.zhai.boot.common.LogConstant;
import com.zhai.boot.common.ServiceResult;
import com.zhai.boot.dao.mongo.ZhaiMongoDao;
import com.zhai.boot.domain.in.ZhaiSaveIn;
import com.zhai.boot.domain.mongo.ZhaiMongo;
import com.zhai.boot.util.JSONUtil;
import com.zhai.boot.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @package: com.zhai.boot.service
 * @project-name: zhai-boot
 * @description: zhai 服务接口实现
 * @author: Created by 翟凤玺
 * @create-datetime: 2018-8-7 14:07
 */
@Slf4j
@Service
public class ZhaiServiceImpl implements ZhaiService {

    private static final String BUSINESS_NAME = "【zhai服务】";
    private static final String SAVE_METHOD = "（保存）";
    private static final String GET_BY_ID_METHOD = "（根据ID查询）";

    @Autowired
    private ZhaiMongoDao zhaiMongoDao;

    @Override
    public ServiceResult<Object> save(ZhaiSaveIn in) {
        log.info(BUSINESS_NAME + LogConstant.SERVICE + SAVE_METHOD + LogConstant.IN + JSONUtil.toStr(in));
        ZhaiMongo insert = new ZhaiMongo();
        ObjectUtil.copy(in, insert);
        insert.setCreateTime(new Date());
        zhaiMongoDao.insert(insert);
        log.info(BUSINESS_NAME + LogConstant.RESULT + SAVE_METHOD + LogConstant.SUCCESS);
        return ServiceResult.success();
    }

    @Override
    public ServiceResult<Object> getById(String id) {
        log.info(BUSINESS_NAME + LogConstant.SERVICE + GET_BY_ID_METHOD + LogConstant.IN + id);
        ZhaiMongo result = zhaiMongoDao.getById(id);
        log.info(BUSINESS_NAME + LogConstant.RESULT + GET_BY_ID_METHOD + LogConstant.SUCCESS + LogConstant.OUT + JSONUtil.toStr(result));
        return ServiceResult.success(result);
    }
}
